import time
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta


def getBetweenDay(begin_date, end_date):
	date_list = []
	begin_date = datetime.strptime(begin_date, "%Y-%m-%d")
	end_date = datetime.strptime(end_date, "%Y-%m-%d")
	while begin_date <= end_date:
		date_str = begin_date.strftime("%Y-%m-%d")
		date_list.append(date_str)
		begin_date += timedelta(days=1)
	return date_list


def getBetweenYear(begin_year, end_year):
	"""
	年份区间之内的所有年 [）左闭右开
	:param begin_year:
	:param end_year:
	:return:
	"""
	begin_date = datetime.strptime(begin_year, "%Y")
	end_date = datetime.strptime(end_year, "%Y")
	date_list = range(begin_date.year, end_date.year + 1)
	return date_list


def getYearSub(date, subYears):
	"""
	日期年份作减
	"""
	d = datetime.strptime(date, '%Y-%m-%d')
	print(d)
	print((d - relativedelta(years=subYears)).strftime('%Y-%m-%d'))


if __name__ == "__main__":
	# str = '2021-10-07 to 2022-01-03'
	# str = str.replace(" ", "")
	# strlist = str.split('to')
	# for i in range(len(strlist)):
	# 	print(strlist[i])
	# yearlist = getBetweenYear('2020', '2022')
	# for i in range(len(yearlist)):
	# 	print(yearlist[i])
	getYearSub('2021-01-01',1)
# res_list = getBetweenDay('2021-10-07', '2022-01-03')
# for i in range(len(res_list)):
# 	print(res_list[i])
